<template>
	<view class="content">
		<uni-section title="检查点" type="line"></uni-section>
		<uni-card :is-shadow="false" padding="10px 0">
			<view>类别：{{dataInfo.type.title}}</view>
			<view>楼层：{{dataInfo.floor}}</view>
		</uni-card>
		
		<view v-if="dataInfo.inspect != null">
			<uni-section title="检查内容" type="line"></uni-section>
			<uni-card :is-shadow="false" padding="10px 0">
				<uni-section :title="item.title" v-for="(item,index) in content" :key="index">
					<template v-slot:decoration><view class="decoration"></view></template>
					<template v-slot:right><uni-tag :text="item.status == '1' ? '正　常' : '不正常'" :type="item.status == 1 ? 'success' : 'error'" size="mini" :inverted="true" /></template>
				</uni-section>
			</uni-card>
		</view>
		
		<view v-if="dataInfo.inspect == null">
			<uni-card :is-shadow="false" padding="10px 0">
				<uni-section :title="item.title" v-for="(item,index) in content" :key="index">
					<template v-slot:decoration><view class="decoration"></view></template>
					<uni-data-checkbox class="select-check" v-model="item.status" :localdata="status" />
				</uni-section>
			</uni-card>
			
			<uni-section title="检查结果(作为记录结果)" type="line"></uni-section>
			<div class="p15">
				<uni-segmented-control :current="current" :values="items" @clickItem="onClickItem" styleType="button"></uni-segmented-control>
				<uni-forms class="mt15" ref="baseForm" :modelValue="form" label-width="90px">
					<uni-forms-item label="整改负责人" required>
						<uni-data-select v-model="form.correction_id" :localdata="user" @change="changeUser" ></uni-data-select>
					</uni-forms-item>
					<uni-forms-item label="整改期限" required>
						<uni-data-select v-model="form.days" :localdata="days" @change="changeDays" ></uni-data-select>
					</uni-forms-item>
				</uni-forms>
			</div>
			
			<!-- <uni-section title="照片上传" type="line"></uni-section> -->
			
			<view class="p15"><button type="primary" @click="onSave">提交</button></view>
		</view>
		
	</view>
</template>

<script>
import func from '../../../api/func'
import request from '../../../api/request'

	export default {
		data() {
			return {
				id:0,
				status:0,
				dataInfo:{floor:'',title:'',type:{title:''}},
				content:[],
				status:[{text: '正常',value: 1},{text: '不正常',value: 2}],
				form:{},
				current: 3,
				items: ['检查合格', '问题整改'],
				days: [{ value: 1, text: "一天" }, { value: 2, text: "两天" }, { value: 3, text: "三天" },],
				user: [{ value: 1, text: "一天" }, { value: 2, text: "两天" }, { value: 3, text: "三天" },],
			}
		},
		onLoad(option) {
			this.id = option.id
			this.onGetData()
			this.onGetUser()
		},
		methods: {
			onSave(){
				this.form.safe_id = this.id
				this.form.content = JSON.stringify(this.content)
				this.form.images = ''
				let _this = this
				request('safe/inspect','PUT',this.form).then(res => {
					if(res.code != 200){
						uni.showToast({title: res.msg,icon: 'error',duration: 2000})
						return;
					}
					_this.onGetData()
				}).catch(err => {
					console.log(err)
				})
			},
			onClickItem(data){
				this.form.type = data.currentIndex + 1
			},
			changeUser(data){
				this.form.correction_id = data
			},
			changeDays(data){
				this.form.days = data
			},
			onGetUser(){
				let _this = this
				request('project/user','POST',{}).then(res => {
					if(res.code != 200){
						uni.showToast({title: res.msg,icon: 'error',duration: 2000})
						return;
					}
					_this.user = func.map_uniapp_name_arr(res.data)
				}).catch(err => {
					console.log(err)
				})
			},
			onGetData(){
				let _this = this
				request('safe/'+this.id,'GET').then(res => {
					if(res.code != 200){
						uni.showToast({title: res.msg,icon: 'error',duration: 2000})
						return;
					}
					_this.dataInfo = res.data
					if(_this.dataInfo.inspect){
						_this.content = JSON.parse(_this.dataInfo.inspect.content)
					}else{
						_this.content = JSON.parse(_this.dataInfo.type.content)
					}
					console.log(_this.content)
				}).catch(err => {
					console.log(err)
				})
			},
		}
	}
</script>

<style lang="scss">
$uni-success: #18bc37 !default;
.decoration{width: 6px;height: 6px;margin-right: 4px;border-radius: 50%;background-color: $uni-success;}
</style>
